package com.yenlien.traceyun.business.service;

import com.alibaba.fastjson.JSONArray;
import com.yenlien.traceyun.business.dao.CuringDao;
import com.yenlien.traceyun.business.dao.StorageManageCuringDao;
import com.yenlien.traceyun.business.entity.Curing;
import com.yenlien.traceyun.business.entity.StorageManageCuring;
import com.yenlien.traceyun.business.service.rest.usercenter.FileRestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * Created by   on 2020/7/30.
 */
@Service
public class StorageManageCuringService {
    @Autowired
    private StorageManageCuringDao storageManageCuringDao;
    @Autowired
    private FileRestService fileService;
    @Autowired
    private CuringDao curingDao;

    public void save(StorageManageCuring storageManage, List<MultipartFile> files) throws Exception {
        fileService.upload(null, storageManage, files);
        saveStorageManageCuring(storageManage);
    }


    public void save2(StorageManageCuring storageManage) throws Exception {
        saveStorageManageCuring(storageManage);
    }

    @Transactional
    public void saveStorageManageCuring(StorageManageCuring storageManage) throws Exception {
        storageManage.setCreatetime(new Date());
        storageManageCuringDao.save(storageManage);
        if (!StringUtils.isEmpty(storageManage.getCuringList())) {
            List<Curing> curingList = JSONArray.parseArray(storageManage.getCuringList(), Curing.class);
            for (Curing curing : curingList) {
                Curing cu = new Curing();
                cu.setCuringId(storageManage.getId());
                cu.setContent(curing.getContent());
                cu.setStandard(curing.getStandard());
                cu.setCreatetime(new Date());
                curingDao.save(cu);
            }
        }
    }





    public List<StorageManageCuring> findByStorageManageId(String id) {
        List<StorageManageCuring> storageManageCurings = storageManageCuringDao.findByStorageManageId(id);
        if (storageManageCurings.size() > 0) {
            for (StorageManageCuring storageManageCuring : storageManageCurings) {
                storageManageCuring.setCurings(curingDao.findByCuringId(storageManageCuring.getId()));
            }
        }
        return storageManageCurings;
    }

}
